package tests;

import network.Device;
import network.Network;
import network.Rule;

import java.util.HashMap;

public class TriangleConstructor {
    private static Network n = new Network();
    private static HashMap<Integer, String> deviceToName = new HashMap<>();

    private static void addLink(int x, String xs, int y) {
        n.addLink(deviceToName.get(x), deviceToName.get(x) + xs, deviceToName.get(y), deviceToName.get(y));
    }

    public static Network getNetwork(int N) {
        n.addDevice("default");
        for (int i = 0; i < N * 2 + 1; i ++) {
            deviceToName.put(i, "device " + i);
            n.addDevice(deviceToName.get(i));
        }
        for (String name : deviceToName.values()) {
            n.addLink(name, "default", "default", name + "-peer-default");
        }

        for (int i = 0; i < N; i ++) {
            addLink(i * 2, "-egress", i * 2 + 1);
            addLink(i * 2, "-egress", i * 2 + 2);
            addLink(i * 2 + 1, "-egress", i * 2 + 2);
        }

        for (int i = 0; i < N * 2; i ++) {
            String name = deviceToName.get(i);
            Device device = n.getNameToDevice().get(name);
            Rule rule = new Rule(device, 1, 32, device.getPortByName(name + "-egress"));
            n.addInitialRules(rule);
        }

        return n;
    }

}
